<template>
  <div class="bigBox">
    <div class="topBox">
      <div class="topBoxCon">
        <div class="title">
          <div class="blueLine">
            计划入商品种类数
          </div>
        </div>
        <div class="right">
          <div class="imgBox" style="background-color: #a8dbd0;">
            <img style="width:30px;height:30px;text-align: center;" src="@/assets/images/rkds.png" alt="" />
          </div>
          <div>{{planInNumList[0]&&planInNumList[0].PLan_In_Num?planInNumList[0].PLan_In_Num:0}}</div>
        </div>
      </div>
      <div class="topBoxCon">
        <div class="title">
          <div class="blueLine">
            计划入商品数
          </div>
        </div>
        <div class="right">
          <div class="imgBox" style="background-color: #ebd1ec;">
            <img style="width:30px;height:30px;text-align: center;" src="@/assets/images/rkzs.png" alt="" />
          </div>
          <div>{{planInNumList[0]&&planInNumList[0].Plan_In_Sku_num?planInNumList[0].Plan_In_Sku_num:0}}</div>
        </div>
      </div>
      <div class="topBoxCon">
        <div class="title">
          <div class="blueLine">
            计划出商品种类数
          </div>
        </div>
        <div class="right">
          <div class="imgBox">
            <img style="width:30px;height:30px;text-align: center;" src="@/assets/images/bhe.png" alt="" />
          </div>
          <div>{{planOutNumList[0]&&planOutNumList[0].PLan_Out_Num?planOutNumList[0].PLan_Out_Num:0}}</div>
        </div>
      </div>
      <div class="topBoxCon">
        <div class="title">
          <div class="blueLine">
            计划出商品数
          </div>
        </div>
        <div class="right">
          <div class="imgBox" style="background-color: #FFCCCC;">
            <img style="width:30px;height:30px;text-align: center;" src="@/assets/images/cppl.png" alt="" />
          </div>
          <div>{{planOutNumList[0]&&planOutNumList[0].Plan_Out_Sku_num?planOutNumList[0].Plan_Out_Sku_num:0}}</div>
        </div>
      </div>
      <div class="topBoxCon">
        <div class="title">
          <div class="blueLine">
            库存中商品种类数
          </div>
        </div>
        <div class="right">
          <div class="imgBox" style="background-color: #D8B0A4;">
            <img style="width:30px;height:30px;text-align: center;" src="@/assets/images/djkc.png" alt="" />
          </div>
          <div>{{skuList[0]&&skuList[0].Sku_Class_Num?skuList[0].Sku_Class_Num:0}}</div>
        </div>
      </div>
      <div class="topBoxCon">
        <div class="title">
          <div class="blueLine">
            商品总量
          </div>
        </div>
        <div class="right">
          <div class="imgBox" style="background-color: #CCE5FF;">
            <img style="width:30px;height:30px;text-align: center;" src="@/assets/images/djsl.png" alt="" />
          </div>
          <div>{{skuList[0]&&skuList[0].Sku_All_Num?skuList[0].Sku_All_Num:0}}</div>
        </div>
      </div>
      <div class="topBoxCon">
        <div class="title">
          <div class="blueLine">
            冻结数量
          </div>
        </div>
        <div class="right">
          <div class="imgBox" style="background-color: #FFCC66;">
            <img style="width:30px;height:30px;text-align: center;" src="@/assets/images/dqks.png" alt="" />
          </div>
          <div>{{skuList[0]&&skuList[0].Freeze_Num?skuList[0].Freeze_Num:0}}</div>
        </div>
      </div>
      <div class="leftBox">
        <div class="title">
          <div class="blueLine">
            商品分类
          </div>
          <el-dropdown @command="skuclasshandleCommand">
            <span class="el-dropdown-link">
              {{skuclassTxt}}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="1">等于</el-dropdown-item>
              <el-dropdown-item command="2">不等于</el-dropdown-item>
              <el-dropdown-item command="0">全部</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <div class="right">
          <el-select multiple v-model="formList.skuclassId" filterable placeholder="请选择商品分类" clearable
            :style="{ width: '100%' }" @change="changeTypeId">
            <el-option v-for="item in goodsTypeList" :label="`${item.ncc_stu_name}(${item.ncc_stu_code})`"
              :value="item.pk_marbasclass" />
          </el-select>
        </div>
      </div>
      <div class="leftBox">
        <div class="title">
          <div class="blueLine">
            仓库
          </div>
          <div>
            <el-dropdown @command="handleCommand">
              <span class="el-dropdown-link">
                {{chooseSto}}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="1">等于</el-dropdown-item>
                <el-dropdown-item command="2">不等于</el-dropdown-item>
                <el-dropdown-item command="0">全部</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <div class="right">
          <el-select multiple v-model="formList.wareHouseID" placeholder="请选择仓库" clearable :style="{ width: '100%' }"
            filterable @change="getWareHouse">
            <el-option v-for="item in warehousesList" :label="item.cwarehouse_name" :value="item.cwarehouse_id" />
          </el-select>
        </div>
      </div>
      <div class="leftBox">
        <div class="title">
          <div class="blueLine">
            产品
          </div>
          <div>
            <el-dropdown @command="goodshandleCommand">
              <span class="el-dropdown-link">
                {{chooseSku}}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="1">等于</el-dropdown-item>
                <el-dropdown-item command="2">不等于</el-dropdown-item>
                <el-dropdown-item command="0">全部</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <div class="right">
          <el-select multiple v-model="formList.skuID" placeholder="请选择产品" clearable :style="{ width: '100%' }"
            filterable @change="getGoods">
            <el-option v-for="item in goodsTotalList" :label="item.material_name" :value="item.pk_material" />
          </el-select>
        </div>
      </div>
      <div class="leftBox">
        <div class="title">
          <div class="blueLine">
            批号
          </div>
          <div>
            <el-dropdown @command="batchhandleCommand">
              <span class="el-dropdown-link">
                {{batchName}}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="1">等于</el-dropdown-item>
                <el-dropdown-item command="2">不等于</el-dropdown-item>
                <el-dropdown-item command="0">全部</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <div class="right">
          <el-input v-model="formList.batchNo" placeholder="请输入批号" style="width: 100%;"></el-input>
        </div>
      </div>
      <div class="leftBox">
        <div class="title">
          <div class="blueLine">
            有效期天数
          </div>
          <div>
            <el-dropdown @command="validityhandleCommand">
              <span class="el-dropdown-link">
                {{validity}}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="1">等于</el-dropdown-item>
                <el-dropdown-item command="2">不等于</el-dropdown-item>
                <el-dropdown-item command="0">全部</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <div class="right">
          <el-input v-model="formList.expDateNum" placeholder="有效期天数" style="width: 100%;"></el-input>
        </div>
      </div>
      <div class="leftBox">
        <div class="title">
          <div class="blueLine">
            复验期天数
          </div>
          <div>
            <el-dropdown @command="retesthandleCommand">
              <span class="el-dropdown-link">
                {{retest}}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="1">等于</el-dropdown-item>
                <el-dropdown-item command="2">不等于</el-dropdown-item>
                <el-dropdown-item command="0">全部</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <div class="right">
          <el-input v-model="formList.resetDateNum" placeholder="复验期天数" style="width: 100%;"></el-input>
        </div>
      </div>
      <div class="leftBox">
        <div class="title">
          <div class="blueLine">
            低库存数量
          </div>
          <div>
            <el-dropdown @command="lowInventoryhandleCommand">
              <span class="el-dropdown-link">
                {{lowInventory}}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="1">等于</el-dropdown-item>
                <el-dropdown-item command="2">不等于</el-dropdown-item>
                <el-dropdown-item command="0">全部</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <div class="right">
          <el-input v-model="formList.stockNum" placeholder="低库存数量" style="width: 100%;"></el-input>
        </div>
      </div>
      <div class="leftBox">
        <div class="title">
          <div class="blueLine">
            商品状态
          </div>
          <el-dropdown @command="skuStatehandleCommand">
            <span class="el-dropdown-link">
              {{skuStateTxt}}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="1">等于</el-dropdown-item>
              <el-dropdown-item command="2">不等于</el-dropdown-item>
              <el-dropdown-item command="0">全部</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <div class="right">
          <el-select filterable multiple v-model="formList.skuState" placeholder="请选择商品状态" clearable
            :style="{ width: '100%' }" @change="goodsStateMethod">
            <el-option v-for="item in goodsState" :label="item.VNAME" :value="item.PK_STORESTATE" />
          </el-select>
        </div>
      </div>
    </div>
    <el-row>
      <el-col :span="24">
        <div class="areaChart">
          <totalChart :totalChartNum="totalChartNum" />
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="areaChart">
          <el-button style="margin: 10px 10px 10px 0" type="primary" icon="el-icon-download"
            @click="exportCSV">导出CSV</el-button>
          <el-button style="margin: 10px 0;" type="primary" icon="el-icon-download"
            @click="exportExcel">导出excel</el-button>
          <el-button style="margin: 10px 0 10px 10px;" type="primary" icon="el-icon-download"
            @click="exportPdf">导出pdf</el-button>
          <totalTable :tableList="tableList" :totalSum="totalSum" :tableKey="tableKey" :pageNum="pageNum" :pageSize="pageSize"/>
          <el-pagination background layout=" total,prev, pager, next, sizes,jumper" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" :current-page="pageNum" :page-size="pageSize"
            :page-sizes="[10, 20, 50, 100]" :total="total">
          </el-pagination>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import {
    getWarehouseStatistic, //获取统计数量
    getWarehouseBasic, //获取商品和仓库下拉
    getBillStatusAll //获取商品状态
  } from "@/api/report/index.js";
  import totalTable from './other/totalTable.vue'
  import totalChart from './other/totalChart.vue'
  export default {
    name: 'totalBank',
    data() {
      return {
        pageNum: 1,
        pageSize: 10,
        total:0,
        tableKey: Date.now(), // 用于强制刷新表格
        totalSum: 0, //合计
        formList: {
          skuclassId: [],
          wareHouseID: [],
          wareHouseValue: 0,
          skuID: [],
          skuIDValue: 0,
          batchNo: '',
          batchNoValue: 0,
          expDateNum: '',
          expDateValue: 0,
          resetDateNum: '',
          resetDateValue: 0,
          stockNum: '',
          stocValue: 0,
          skuState: [], //商品状态
          skuStateValue: 0,
          skuclassValue: 0
        },
        chooseSto: '全部', //仓库下拉
        chooseSku: '全部', //产品下拉
        batchName: '全部', //批号下拉
        lowInventory: '全部', //低库存下拉
        retest: '全部', //复验期下拉
        validity: '全部', //有效期下拉
        skuStateTxt: '全部', //商品状态下拉
        skuclassTxt: '全部', //商品分类下拉
        warehousesList: [],
        goodsTotalList: [],
        planInNumList: [], //计划入的商品种类数量&计划入商品数量
        planOutNumList: [], //计划出的商品种类数量&计划出商品数量
        skuList: [], //库存中现在商品种类数量&商品总量&冻结数量
        goodsState: [], //获取商品状态
        goodsTypeList: [], //获取商品分类
        tableList: [], //库存明细数据
        totalChartNum: [], //前十名统计
        numItems: [{
            key: 'unqualified_num',
            label: '不合格'
          },
          {
            key: 'quarantine_num',
            label: '待检'
          },
          {
            key: 'qualified_num',
            label: '合格'
          },
          {
            key: 'limit_eligibility_num',
            label: '限制合格'
          },
          {
            key: 'sku_no_state_num',
            label: '无库存状态'
          },
          {
            key: 'research_num',
            label: '研究用'
          },
          {
            key: 'qualified_no_use_num',
            label: '合格(不上市)'
          },
          {
            key: 'pending_num',
            label: '待处理'
          }
        ]
      }
    },
    components: {
      totalTable,
      totalChart
    },
    created() {
      this.getData();
      this.getTotal();
      this.getGoodsState();
    },
    methods: {
      //分页
      handleSizeChange(newsize) {
        this.pageNum = 1;
        this.pageSize = newsize;
      },
      handleCurrentChange(newpage) {
        this.pageNum = newpage;
      },
      //导出CSV
      exportCSV() {
        const params = {
          ...this.formList,
          skuclassId: this.formList.skuclassId.join(','),
          wareHouseID: this.formList.wareHouseID.join(','),
          skuID: this.formList.skuID.join(','),
          skuState: this.formList.skuState.join(','),
          pageNum:this.pageNum,
          pageSize:this.pageSize
        };
        this.download('warehouse/exportCsvWareHouseStatistic', {
          ...params
        }, `库存统计.csv`)
      },
      //初始化基础数据
      getData() {
        getWarehouseBasic().then(res => {
          this.warehousesList = res.data.type1 || []; //仓库下拉
          this.goodsTotalList = res.data.type2 || []; //产品下拉
          this.goodsTypeList = res.data.type3 || []; //商品分类下拉
        }).catch(error => {
          this.$message.error('获取仓库和产品数据失败:', error);
        });
      },
      getGoodsState() {
        getBillStatusAll().then(res => {
          this.goodsState = res.data;
        })
      },
      //导出pdf
      exportPdf() {
        const params = {
          ...this.formList,
          skuclassId: this.formList.skuclassId.join(','),
          wareHouseID: this.formList.wareHouseID.join(','),
          skuID: this.formList.skuID.join(','),
          skuState: this.formList.skuState.join(','),
          pageNum:this.pageNum,
          pageSize:this.pageSize
        };
        this.download('warehouse/exportPdfWareHouseStatistic', {
          ...params
        }, `库存统计.pdf`)
      },
      //打印excel
      exportExcel() {
        const params = {
          ...this.formList,
          skuclassId: this.formList.skuclassId.join(','),
          wareHouseID: this.formList.wareHouseID.join(','),
          skuID: this.formList.skuID.join(','),
          skuState: this.formList.skuState.join(','),
          pageNum:this.pageNum,
          pageSize:this.pageSize
        };
        this.download('warehouse/exportWareHouseStatistic', {
          ...params
        }, `库存统计.xlsx`)
      },
      //入库统计
      async getTotal() {
        const params = {
          ...this.formList,
          skuclassId: this.formList.skuclassId.join(','),
          wareHouseID: this.formList.wareHouseID.join(','),
          skuID: this.formList.skuID.join(','),
          skuState: this.formList.skuState.join(',')
        };
        const res = await getWarehouseStatistic(params);
        if (res.data) {
          this.planInNumList = res.data.type0 || [];
          this.planOutNumList = res.data.type1 || [];
          this.skuList = res.data.type2 || [];
          this.tableList = res.data.type4 || [];
          this.total = res.data.type4.length;
          this.totalSum = res.data.type4.total_quantity ||
            (res.data.type4.length > 0 ? res.data.type4[0].total_quantity : 0);
          this.totalChartNum = res.data.type3 || []
        }
        this.tableKey = Date.now(); // 强制刷新表格
      },
      //切换仓库
      getWareHouse(val) {
        this.formList.wareHouseID = val;
        this.getTotal();
      },
      //切换产品
      getGoods(val) {
        this.formList.skuID = val;
        this.getTotal();
      },
      //切换商品状态
      goodsStateMethod(val) {
        this.formList.skuState = val;
        this.getTotal();
      },
      //切换商品分类
      changeTypeId(val) {
        this.formList.skuclassId = val;
        this.getTotal();
      },
      //商品分类下拉
      skuclasshandleCommand(command) {
        switch (command) {
          case '1':
            this.skuclassTxt = '等于';
            this.formList.skuclassValue = command;
            this.getTotal();
            break;
          case '2':
            this.skuclassTxt = '不等于';
            this.formList.skuclassValue = command;
            this.getTotal();
            break;
          case '0':
            this.skuclassTxt = '全部';
            this.formList.skuclassValue = command;
            this.getTotal();
            break;
        }
      },
      //商品状态下拉
      skuStatehandleCommand(command) {
        switch (command) {
          case '1':
            this.skuStateTxt = '等于';
            this.formList.skuStateValue = command;
            this.getTotal();
            break;
          case '2':
            this.skuStateTxt = '不等于';
            this.formList.skuStateValue = command;
            this.getTotal();
            break;
          case '0':
            this.skuStateTxt = '全部';
            this.formList.skuStateValue = command;
            this.getTotal();
            break;
        }
      },
      //仓库下拉
      handleCommand(command) {
        switch (command) {
          case '1':
            this.chooseSto = '等于';
            this.formList.wareHouseValue = command;
            this.getTotal();
            break;
          case '2':
            this.chooseSto = '不等于';
            this.formList.wareHouseValue = command;
            this.getTotal();
            break;
          case '0':
            this.chooseSto = '全部';
            this.formList.wareHouseValue = command;
            this.getTotal();
            break;
        }
      },
      //产品下拉
      goodshandleCommand(command) {
        switch (command) {
          case '1':
            this.chooseSku = '等于';
            this.formList.skuIDValue = command;
            this.getTotal();
            break;
          case '2':
            this.chooseSku = '不等于';
            this.formList.skuIDValue = command;
            this.getTotal();
            break;
          case '0':
            this.chooseSku = '全部';
            this.formList.skuIDValue = command;
            this.getTotal();
            break;
        }
      },
      //批号下拉
      batchhandleCommand(command) {
        switch (command) {
          case '1':
            this.batchName = '等于';
            this.formList.batchNoValue = command;
            this.getTotal();
            break;
          case '2':
            this.batchName = '不等于';
            this.formList.batchNoValue = command;
            this.getTotal();
            break;
          case '0':
            this.batchName = '全部';
            this.formList.batchNoValue = command;
            this.getTotal();
            break;
        }
      },
      //有效期下拉
      validityhandleCommand(command) {
        switch (command) {
          case '1':
            this.validity = '等于';
            this.formList.expDateValue = command;
            this.getTotal();
            break;
          case '2':
            this.validity = '不等于';
            this.formList.expDateValue = command;
            this.getTotal();
            break;
          case '0':
            this.validity = '全部';
            this.formList.expDateValue = command;
            this.getTotal();
            break;
        }
      },
      //复验期下拉
      retesthandleCommand(command) {
        switch (command) {
          case '1':
            this.retest = '等于';
            this.formList.resetDateValue = command;
            this.getTotal();
            break;
          case '2':
            this.retest = '不等于';
            this.formList.resetDateValue = command;
            this.getTotal();
            break;
          case '0':
            this.retest = '全部';
            this.formList.resetDateValue = command;
            this.getTotal();
            break;
        }
      },
      //低库存下拉
      lowInventoryhandleCommand(command) {
        switch (command) {
          case '1':
            this.lowInventory = '等于';
            this.formList.stocValue = command;
            this.getTotal();
            break;
          case '2':
            this.lowInventory = '不等于';
            this.formList.stocValue = command;
            this.getTotal();
            break;
          case '0':
            this.lowInventory = '全部';
            this.formList.stocValue = command;
            this.getTotal();
            break;
        }
      },
    },
  }
</script>
<style scoped lang="scss">
  .el-pagination {
    float: right;
    margin: 20px;
  }

  .topBox {
    flex-wrap: wrap;
    display: flex;
    gap: 0 15px;
  }

  //曲线图
  .areaChart {
    border: 1px solid #e1e1e1;
    padding: 10px;
    border-radius: 10px;
    box-shadow: 5px 5px 10px 0px rgba(0, 0, 0, 0.1);
  }

  .blueLine {
    display: flex;
    align-items: center;
  }

  //下拉菜单
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }

  .el-icon-arrow-down {
    font-size: 12px;
  }

  .blueLine:before {
    content: "";
    background: #8cc5ff;
    display: block;
    width: 6px;
    height: 20px;
    margin-right: 10px;

  }

  .leftBox:nth-child(2n) .right {
    display: flex;
    align-items: center;
    justify-content: space-around;
  }

  .imgBox {
    width: 50px;
    height: 50px;
    border-radius: 5px;
    background-color: #eef7ff;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .topBoxCon {
    width: calc(14.28% - 13px) !important;
  }

  .leftBox,
  .topBoxCon {
    margin-bottom: 10px;
    padding: 0 10px;
    height: 150px;
    width: calc(25% - 12px);
    border-radius: 10px;
    border: 1px solid #e1e1e1;
    box-shadow: 5px 5px 10px 0px rgba(0, 0, 0, 0.1);
    flex-wrap: wrap;

    .title {
      justify-content: space-between;
      display: flex;
      align-items: center;
      color: #333333;
      font-weight: bold;
      padding: 10px 0px;
      font-size: 18px;
    }

    .right {
      display: flex;
      align-items: center;
      justify-content: space-evenly;
      font-size: 30px;
      color: #409EFF;
      line-height: 100px;
      font-weight: bold;
    }
  }

  .el-radio-group {
    line-height: 100px;
  }

  .el-row {
    margin-top: 10px;
  }

  // ::v-deep .el-input--suffix {
  //   height: 70px;
  // }

  @media (max-width: 1500px) {

    .leftBox,
    .topBoxCon {
      width: calc(25% - 12px) !important;
      /* 每行4个卡片 */
    }
  }

  @media (max-width: 1200px) {

    .leftBox,
    .topBoxCon {
      width: calc(50% - 12px) !important;
      /* 每行2个卡片 */
    }
  }

  @media (max-width: 480px) {

    .leftBox,
    .topBoxCon {
      width: 100% !important;
      /* 每行1个卡片 */
    }
  }
</style>
